package org.jeecg.modules.zhaojd.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.activiti.engine.IdentityService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.Model;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.JsonUtils;
import org.jeecg.modules.hqbaoxiao.entity.HqBaoxiaoDetail;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.service.ISysUserService;
import org.jeecg.modules.zhaojd.entity.HqZhaojd;
import org.jeecg.modules.zhaojd.entity.HqZhaojdDetail;
import org.jeecg.modules.zhaojd.entity.HqZhaojdChif;
import org.jeecg.modules.zhaojd.mapper.HqZhaojdDetailMapper;
import org.jeecg.modules.zhaojd.mapper.HqZhaojdChifMapper;
import org.jeecg.modules.zhaojd.mapper.HqZhaojdMapper;
import org.jeecg.modules.zhaojd.service.IHqZhaojdService;
import org.jeecg.modules.zhaojd.vo.HqZhaojdPage;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Collection;
import java.util.Map;

/**
 * @Description: 招接待
 * @Author: jeecg-boot
 * @Date:   2020-10-05
 * @Version: V1.0
 */
@Service
public class HqZhaojdServiceImpl extends ServiceImpl<HqZhaojdMapper, HqZhaojd> implements IHqZhaojdService {

	@Autowired
	private HqZhaojdMapper hqZhaojdMapper;
	@Autowired
	private HqZhaojdDetailMapper hqZhaojdDetailMapper;
	@Autowired
	private HqZhaojdChifMapper hqZhaojdChifMapper;

	@Autowired
	private IdentityService identityservice;
	@Autowired
	private RuntimeService runtimeservice;
	@Autowired
	private TaskService taskService;
	@Autowired
	private RepositoryService repositoryService;
	@Autowired
	private ISysUserService userService;
	
	@Override
	@Transactional
	public void startFlow(LoginUser user,HqZhaojd hqZhaojd, List<HqZhaojdDetail> hqZhaojdDetailList,List<HqZhaojdChif> hqZhaojdChifList) {
		hqZhaojdMapper.insert(hqZhaojd);
		if(hqZhaojdDetailList!=null && hqZhaojdDetailList.size()>0) {
			for(HqZhaojdDetail entity:hqZhaojdDetailList) {
				//外键设置
				entity.setZhubId(hqZhaojd.getId());
				hqZhaojdDetailMapper.insert(entity);
			}
		}
		if(hqZhaojdChifList!=null && hqZhaojdChifList.size()>0) {
			for(HqZhaojdChif entity:hqZhaojdChifList) {
				//外键设置
				entity.setZhubId(hqZhaojd.getId());
				hqZhaojdChifMapper.insert(entity);
			}
		}

		List<SysUser> userList = userService.queryByPost(user.getOrgCode(),"shenpiren");
		if(CollectionUtils.isEmpty(userList)){
			throw new RuntimeException("部门负责人为空");
		}

		String businesskey = "" + hqZhaojd.getId();

		identityservice.setAuthenticatedUserId(user.getUsername());

		Map<String, Object> variables = new HashMap<String, Object>();
		variables.put("applyUser", user.getUsername());
		variables.put("auditUser",userList.get(0).getUsername());
		Model modelData = repositoryService.getModel("367520");
		ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().deploymentId(modelData.getDeploymentId()).singleResult();

		ProcessInstance instance=runtimeservice.startProcessInstanceById(processDefinition.getId(),businesskey,variables);
		String instanceid=instance.getId();
		hqZhaojd.setProcessInstanceId(instanceid);
		Task task = taskService.createTaskQuery().processInstanceId(instance.getId()).taskAssignee(user.getUsername()).singleResult();
		hqZhaojd.setTaskId(task.getId());
		this.baseMapper.updateById(hqZhaojd);
		taskService.complete(task.getId(),variables);
	}


	@Override
	@Transactional
	public void saveMain(HqZhaojd hqZhaojd, List<HqZhaojdDetail> hqZhaojdDetailList,List<HqZhaojdChif> hqZhaojdChifList) {
		hqZhaojdMapper.insert(hqZhaojd);
		if(hqZhaojdDetailList!=null && hqZhaojdDetailList.size()>0) {
			for(HqZhaojdDetail entity:hqZhaojdDetailList) {
				//外键设置
				entity.setZhubId(hqZhaojd.getId());
				hqZhaojdDetailMapper.insert(entity);
			}
		}
		if(hqZhaojdChifList!=null && hqZhaojdChifList.size()>0) {
			for(HqZhaojdChif entity:hqZhaojdChifList) {
				//外键设置
				entity.setZhubId(hqZhaojd.getId());
				hqZhaojdChifMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(HqZhaojd hqZhaojd,List<HqZhaojdDetail> hqZhaojdDetailList,List<HqZhaojdChif> hqZhaojdChifList) {
		hqZhaojdMapper.updateById(hqZhaojd);
		
		//1.先删除子表数据
		hqZhaojdDetailMapper.deleteByMainId(hqZhaojd.getId());
		hqZhaojdChifMapper.deleteByMainId(hqZhaojd.getId());
		
		//2.子表数据重新插入
		if(hqZhaojdDetailList!=null && hqZhaojdDetailList.size()>0) {
			for(HqZhaojdDetail entity:hqZhaojdDetailList) {
				//外键设置
				entity.setZhubId(hqZhaojd.getId());
				hqZhaojdDetailMapper.insert(entity);
			}
		}
		if(hqZhaojdChifList!=null && hqZhaojdChifList.size()>0) {
			for(HqZhaojdChif entity:hqZhaojdChifList) {
				//外键设置
				entity.setZhubId(hqZhaojd.getId());
				hqZhaojdChifMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		hqZhaojdDetailMapper.deleteByMainId(id);
		hqZhaojdChifMapper.deleteByMainId(id);
		hqZhaojdMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			hqZhaojdDetailMapper.deleteByMainId(id.toString());
			hqZhaojdChifMapper.deleteByMainId(id.toString());
			hqZhaojdMapper.deleteById(id);
		}
	}

//	@Override
//	public void startFlow(HqZhaojd hqZhaojd, LoginUser user) {
//		List<SysUser> userList = userService.queryByPost(user.getOrgCode(),"shenpiren");
//		if(CollectionUtils.isEmpty(userList)){
//			throw new RuntimeException("部门负责人为空");
//		}
//
//		String businesskey = "" + hqZhaojd.getId();
//
//		identityservice.setAuthenticatedUserId(user.getUsername());
//
//		Map<String, Object> variables = new HashMap<String, Object>();
//		variables.put("applyUser", user.getUsername());
//		variables.put("auditUser",userList.get(0).getUsername());
//		Model modelData = repositoryService.getModel("367520");
//		ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().deploymentId(modelData.getDeploymentId()).singleResult();
//
//		ProcessInstance instance=runtimeservice.startProcessInstanceById(processDefinition.getId(),businesskey,variables);
//		String instanceid=instance.getId();
//		hqZhaojd.setProcessInstanceId(instanceid);
//		Task task = taskService.createTaskQuery().processInstanceId(instance.getId()).taskAssignee(user.getUsername()).singleResult();
//		hqZhaojd.setTaskId(task.getId());
//		this.baseMapper.updateById(hqZhaojd);
//		taskService.complete(task.getId(),variables);
//
//	}

	@Override
	public void complateTask(LoginUser loginUser, String taskId, String processInstanceId, Integer status, String memo) {
		if(StringUtils.isBlank(memo)){
			memo = status == 1?"通过":"驳回";
		}

		Map map = new HashMap(){{
			put("status",status);
		}};

		map.put("memo",memo);
		taskService.addComment(taskId,processInstanceId, JsonUtils.toJsonString(map));
		taskService.complete(taskId,map);
	}

	@Override
	@Transactional
	public void applyAgain(HqZhaojdPage hqZhaojdPage, LoginUser user) {
		HqZhaojd hqZhaojd = new HqZhaojd();
		BeanUtils.copyProperties(hqZhaojdPage, hqZhaojd);

		Task task = taskService.createTaskQuery().processInstanceId(hqZhaojd.getProcessInstanceId()).taskAssignee(user.getUsername()).singleResult();
			hqZhaojd.setTaskId(task.getId());
		hqZhaojd.setCreateTime(null);
		this.baseMapper.updateById(hqZhaojd);

		//1.先删除子表数据
		hqZhaojdDetailMapper.deleteByMainId(hqZhaojd.getId());
		hqZhaojdChifMapper.deleteByMainId(hqZhaojd.getId());

		List<HqZhaojdDetail> hqZhaojdDetailList = hqZhaojdPage.getHqZhaojdDetailList();
		//2.子表数据重新插入
		if(hqZhaojdDetailList!=null && hqZhaojdDetailList.size()>0) {
			for(HqZhaojdDetail entity:hqZhaojdDetailList) {
				//外键设置
				entity.setZhubId(hqZhaojd.getId());
				hqZhaojdDetailMapper.insert(entity);
			}
		}
		List<HqZhaojdChif> hqZhaojdChifList = hqZhaojdPage.getHqZhaojdChifList();
		if(hqZhaojdChifList!=null && hqZhaojdChifList.size()>0) {
			for(HqZhaojdChif entity:hqZhaojdChifList) {
				//外键设置
				entity.setZhubId(hqZhaojd.getId());
				hqZhaojdChifMapper.insert(entity);
			}
		}

		Map<String, Object> variables = new HashMap<String, Object>();
		variables.put("status",null);
		taskService.complete(task.getId(),variables);
	}

	@Override
	public IPage<HqZhaojd> myApplyList(Page<HqZhaojd> page, LoginUser user, HqZhaojd hqZhaojd) {
		return this.baseMapper.myApplyList(page,user,hqZhaojd);
	}

	@Override
	public IPage<HqZhaojd> myTodoTasks(Page<HqZhaojd> page, LoginUser user, HqZhaojd hqZhaojd) {
		return this.baseMapper.myTodoTasks(page,user,hqZhaojd);
	}

	@Override
	public IPage<HqZhaojd> myDoneTasks(Page<HqZhaojd> page, LoginUser user, HqZhaojd hqZhaojd) {
		return this.baseMapper.myDoneTasks(page,user,hqZhaojd);
	}

}
